package AliDiepPR.ANN;

import java.util.Random;

import AliDiepPR.ANN.Perceptron;

public class Layer {
	double[][] wei;

	boolean outputLayer = false;

	public boolean IsOutputLayer() {
		return outputLayer;
	}

	int numUnit;

	public int NumUnit() {
		return numUnit;
	}

	Perceptron[] unit;

	public Perceptron Unit(int i) {
		return unit[i];
	}

	public double[] Output() {
		double[] output = new double[numUnit];
		for (int i = 0; i < numUnit; i++)
			output[i] = unit[i].Output(0, false);
		return output;
	}

	public Layer(int numUnit) {
		this.numUnit = numUnit;

		unit = new Perceptron[this.numUnit];
		for (int i = 0; i < this.numUnit; i++)
			unit[i] = new Perceptron();
	}

	public void RandomizeWeight(int next, Random rand) {
		wei = new double[next][];
		for (int j = 0; j < next; j++) {
			wei[j] = new double[numUnit];
			for (int i = 0; i < numUnit; i++)
				wei[j][i] = (double) rand.nextDouble();
		}
	}

	public double[] getWeightRow(int row) {
		return wei[row];
	}

	public double getWeight(int row, int col) {
		return wei[row][col];
	}

	public void updateWeight(int next, double[][] theta) {
		for (int j = 0; j < next; j++)
			for (int i = 0; i < numUnit; i++)
				wei[j][i] += theta[j][i];
	}

	public void initWeight(int next, double[][] w) {
		wei = new double[w.length][];
		for (int j = 0; j < next; j++) {
			wei[j] = new double[w[j].length];
			for (int i = 0; i < numUnit; i++)
				wei[j][i] = w[j][i];
		}
	}

	public double[][] PrintWeight(int lay) {
		double[][] ret = new double[wei.length][];
		for (int i = 0; i < wei.length; i++) {
			ret[i] = new double[wei[0].length];
			for (int j = 0; j < wei[0].length; j++)
				ret[i][j] = wei[i][j];
		}
		return ret;
	}
}
